package com.landeb.shop.huo.act;

import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.landeb.shop.R;
import com.landeb.shop.huo.MyApplication;
import com.landeb.shop.huo.bean.BeanActAccountBalance;
import com.landeb.shop.huo.httpRequest.ActAccountBalanceRequest;
import com.landeb.shop.huo.httpRequest.ActMoneyLogRequest;

import java.text.SimpleDateFormat;
import java.util.Date;

import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class AccountBalanceActivity extends BaseActivity {

    private LinearLayout btnBack;
    private Button btnRewardLog;

    private TextView tvR1C1, tvR1C2, tvR1C3, tvR2C1, tvR2C2, tvR2C3;

    private Retrofit mRetrofit;
    private ActAccountBalanceRequest mRequest;


    public static void toAct(Context context) {
        context.startActivity(new Intent(context, AccountBalanceActivity.class));
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_account_balance);

        btnBack = (LinearLayout) findViewById(R.id.btn_back_balance_account_act);
        btnRewardLog = (Button) findViewById(R.id.btn_rewardLog_balance_account_act);

        tvR1C1 = (TextView) findViewById(R.id.tv_r1c1_act_account_balance);
        tvR1C2 = (TextView) findViewById(R.id.tv_r1c2_act_account_balance);
        tvR1C3 = (TextView) findViewById(R.id.tv_r1c3_act_account_balance);
        tvR2C1 = (TextView) findViewById(R.id.tv_r2c1_act_account_balance);
        tvR2C2 = (TextView) findViewById(R.id.tv_r2c2_act_account_balance);
        tvR2C3 = (TextView) findViewById(R.id.tv_r2c3_act_account_balance);

        btnBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });

        btnRewardLog.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                RewardLogActivity.toAct(AccountBalanceActivity.this);
            }
        });

        OkHttpClient client = MyApplication.getClient();

        mRetrofit = new Retrofit.Builder()
                .baseUrl(MyApplication.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .client(client)
                .build();
        mRequest = mRetrofit.create(ActAccountBalanceRequest.class);


        refresh();
    }

    private void refresh() {

        mRequest.getData("1")
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<BeanActAccountBalance>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(BeanActAccountBalance beanActAccountBalance) {


                        tvR1C1.setText(beanActAccountBalance.getData().getMoney());
                        tvR1C2.setText(beanActAccountBalance.getData().getMoney_cost());
                        tvR1C3.setText(beanActAccountBalance.getData().getMoney_time());
                        tvR2C1.setText(beanActAccountBalance.getData().getScore());
                        tvR2C2.setText(beanActAccountBalance.getData().getScore_cost());
                        tvR2C3.setText(beanActAccountBalance.getData().getScore_time());

//                        Log.e( "onNext: ",dateR2C3 );
                    }
                });

    }
}
